﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BegVCSharp_24_7_QueryXMl
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFileName = @"G:\NorthwindCustomerOrders.xml";
            XDocument customers = XDocument.Load(xmlFileName);

            Console.WriteLine("Elements in loaded document: ");
            var queryResult =
                from c in customers.Elements()
                select c.Name;

            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("All descandants in document: ");
            queryResult =
                from c in customers.Descendants()
                select c.Name;
            foreach (var item in queryResult.Distinct())
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Descendants named 'customer': ");
            queryResult =
                from c in customers.Descendants("customer")
                select c.Name;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Attributes of descendants named 'customer'");
            queryResult =
                from c in customers.Descendants("customer").Attributes()
                select c.Name;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("customer attributes named 'Company': ");
            var queryResult1 =
                from c in customers.Descendants("customer").Attributes("Company")
                select c;
            foreach (var item in queryResult1)
            {
                Console.WriteLine(item);
            }

            Console.Write("Press enter/return to continue..");
            Console.ReadLine();

        }
    }
}
